package com.suneee.trades.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.trades.bean.TradesInfoDTO;
import com.suneee.trades.dao.HistoryOrderDao;
import com.suneee.trades.entity.HistoryOrder;
import com.suneee.trades.resource.OrderStatus;
import com.suneee.trades.resource.TradesStatus;

@Repository("historyOrderDao")
public class HistoryOrderDaoImpl extends GenericDao<HistoryOrder, Integer> implements HistoryOrderDao {

	@Override
	public List<HistoryOrder> exportHistoryOrders(TradesInfoDTO dto) {
//		StringBuffer sql = new StringBuffer("select * from v_history_order  ");
		
		List<Object> list = null;
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String, Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		if (null != shopList && shopList.size() > 0) {
			list = new ArrayList<Object>();
			for (Map<String, Object> map : shopList) {
				list.add(map.get("shop_id"));
			}
		}
		if (null == shopList || shopList.size() == 0) {
			list = new ArrayList<Object>();
			list.add(" ");
		}
		WhereClauseBuilder wcb = new WhereClauseBuilder();
		
		wcb.equals("tid", dto.getTid());
		wcb.equals("shop_id", dto.getShopId());
		wcb.in("shop_id", list);
		wcb.equals("buyer_nick", dto.getBuyerNick());
		wcb.equals("enterprise_code",dto.getEnterpriseCode());
		wcb.equals("channel_code",dto.getChannelCode()); //cl.
		wcb.between("pay_time", dto.getPayStartTime(), dto.getPayEndTime());//下单时间
		wcb.between("audit_time", dto.getReviewStartTime(), dto.getReviewEndTime());//审单时间
		wcb.between("consign_time", dto.getShoppStartTime(), dto.getShoppEndTime()); //tto.
		
		String tradeType = dto.getTradeType();
		if (OrderStatus.TRADE_BUYER_SIGNED.name().equals(tradeType)) {
			List<Object> status = new ArrayList<>();
			status.add(TradesStatus.TRADE_BUYER_SIGNED);
			status.add(TradesStatus.TRADE_FINISHED);
			wcb.in("status", status);
		} else if (OrderStatus.REFUND_GOODS.name().equals(tradeType)) {
			wcb.equals("is_refund_goods", true);
		} else if (OrderStatus.CHANGE_GOODS.name().equals(tradeType)) {
			wcb.equals("is_change_goods", true);
		} else if (OrderStatus.REFUND.name().equals(tradeType)) {
			wcb.equals("is_refund", true);
		}else{
			List<Object> status = new ArrayList<>();
			status.add(TradesStatus.TRADE_BUYER_SIGNED);
			status.add(TradesStatus.TRADE_FINISHED);
			wcb.in("status", status);
			wcb.append(" or is_refund = ?", true);//tti.
			wcb.append(" or is_refund_goods = ?", true);
			wcb.append(" or is_change_goods = ?", true);
		}
		
//		sql.append(wcb.getSql());
		
		if (dto.getPayTimeSort() == 0) {
			dto.setSort((dto.getSort() == null || StringUtils.isEmpty(dto.getSort().getOrderByClause())) ? new Sort("pay_time", Order.DESC) : dto.getSort());
		} else {
			dto.setSort((dto.getSort() == null || StringUtils.isEmpty(dto.getSort().getOrderByClause())) ? new Sort("pay_time", Order.ASC) : dto.getSort());
		}
		
		return this.selectWhere(wcb.getSql(), dto.getSort(), wcb.getArgs());
	}

}
